package com.woniu.config;



import springfox.documentation.service.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
	
	@Autowired
	private SwaggerProperties sp;

	/**
	 * 构建swaggger的配置对象
	 * @return
	 */
	@Bean
	public Docket createDocke() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2);
		//设置全局参数
		List<Parameter> pars = new ArrayList<>();
		//创建一个可以携带header的全局参数（token）
		Parameter par = new ParameterBuilder().description("token").name("token")
				 .parameterType("header").modelRef(new ModelRef("string")).build();
		pars.add(par);
		//构建文档的基本信息
		ApiInfo apiInfo = new ApiInfoBuilder()
				.contact(new Contact(sp.getContact().getName(), sp.getContact().getHost(), sp.getContact().getEmail()))
				.title(sp.getTitle())
				.description(sp.getDesc())
				.version(sp.getVersion())
				.build();
		//设置扫描的路径
		return docket.apiInfo(apiInfo)
					 .select()
					   .apis(RequestHandlerSelectors.basePackage(sp.getBasePackages()))
					   .paths(PathSelectors.any())
					   .build()
					   .globalOperationParameters(pars);
	}
}






